package com.bill99.rpc.client;

import java.io.IOException;
import java.net.MalformedURLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bill99.rpc.demo.DemoService;
import com.caucho.hessian.client.HessianProxyFactory;

/**
 * @author sheng.zhang
 */
public class OConsumerServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private Log log = LogFactory.getLog(OConsumerServlet.class);

	private DemoService demoService = null;

	String url = "http://192.168.50.48/rpc-demo/demoService";

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		HessianProxyFactory factory = new HessianProxyFactory();
		if (demoService == null)
			try {
				demoService = (DemoService) factory.create(DemoService.class,
						url);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} // 获取远程服务代理
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String r = demoService.sayHello("world");
		log.info("response: " + r);
	}

}
